/**
 * Copyright Choonho Son ,choonho.son(at)kt(dot)com
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.Hashtable;

import com.oracle.em12c.api.Cloud;
import com.oracle.em12c.api.Element;
import com.oracle.em12c.api.NetworkInterface;
import com.oracle.em12c.api.ServiceTemplate;
import com.oracle.em12c.api.VDC;
import com.oracle.em12c.api.VM;
import com.oracle.em12c.api.VMStatus;
import com.oracle.em12c.api.Zone;
import com.oracle.em12c.command.Command;


public class CloudAPI {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		
		
		String base_url = "https://10.12.5.52:7799";
		String id = "sysman";
		String pw = "New12345";
		
		Command cmd = new Command();
		cmd.setBaseURL(base_url);
		cmd.setAuth(id, pw);
		
		// GetCloud
		Hashtable params = new Hashtable();
		params.put("uri", "/em/cloud");
		Cloud clz = (Cloud) cmd.createCmd("GetCloud", params);
		
		System.out.println("name:" + clz.name);
		System.out.println("description:" + clz.description);
		String zone_uri = clz.zones.elements[0].uri;
		
		/*
		// GetZone
		System.out.println("Zone URI:"+zone_uri);
		Hashtable zone_param = new Hashtable();
		zone_param.put("uri", zone_uri);
		Zone zone = (Zone) cmd.createCmd("GetZone", zone_param);
		System.out.println("Zone name:"+ zone.name);
		
		//GetVDC
		String vdc_uri = clz.vdcs.elements[0].uri;
		Hashtable vdc_param = new Hashtable();
		vdc_param.put("uri", vdc_uri);
		VDC vdc = (VDC) cmd.createCmd("GetVDC", vdc_param);
		System.out.println("VDC name:" + vdc.name);
		System.out.println("Number of VMs:" + vdc.servers.total);
		
		//GetVM
		String vm_uri = vdc.servers.elements[0].uri;
		Hashtable vm_param = new Hashtable();
		vm_param.put("uri", vm_uri);
		VM vm = (VM) cmd.createCmd("GetVM", vm_param);
		System.out.println("VM name:" + vm.name);
		
		*/
		
		//GetNetworkInterface
		/*
		String nw_uri = vm.interfaces.elements[0].uri;
		Hashtable nw_param = new Hashtable();
		nw_param.put("uri", nw_uri);
		NetworkInterface nw = (NetworkInterface) cmd.createCmd("GetNetworkInterface", nw_param);
		System.out.println("NW MAC:" + nw.mac);
		*/
		
		/*
		//GetServiceTemplate
		String st_uri = clz.service_templates.elements[0].uri;
		Hashtable st_param = new Hashtable();
		st_param.put("uri", st_uri);
		ServiceTemplate st = (ServiceTemplate) cmd.createCmd("GetServiceTemplate", st_param);
		System.out.println("Service Template Name:" + st.name);
		
		//Control VM
		//String vm_uri = vdc.servers.elements[0].uri;
		Hashtable vm2_param = new Hashtable();
		vm2_param.put("uri", vm_uri);
		vm2_param.put("status", "STARTED");
		VMStatus vm2 = (VMStatus) cmd.createCmd("ControlVM", vm2_param);
		System.out.println("VM name:" + vm2.resource_state);
	
		//DeployVM
		String based_on = (String)params.get("based_on");
		String server_prefix = (String)params.get("server_prefix");
		String vnc_password = (String)params.get("vnc_password");
		String root_password = (String)params.get("root_password");
		String server_size = (String)params.get("server_size");
		
		//deployVM("/em/cloud/servicetemplate/oracle:defaultService:em:provisioning:1:cmp:Virtualization:Template:C40D00098C095E25E0400C0A3305078C:0.1");
		Hashtable deploy_param = new Hashtable();
		deploy_param.put("uri", vdc.uri);
		deploy_param.put("based_on", st.uri);
		deploy_param.put("server_prefix", "My API Server 3");
		deploy_param.put("vnc_password", "password");
		deploy_param.put("root_password", "password");
		deploy_param.put("server_size", "Medium");
		VMStatus vm3 = (VMStatus) cmd.createCmd("DeployVM", deploy_param);
		*/
	}
}
